<template>
  <div>
    <!-- 新增按钮  ---新增数据 -->
    <!-- 点击新增按钮,弹出el-dialog对话框
    el-dialog对话框中嵌套el-form
    el-form中的表单有三个     三个el-input  问题内容  问题回答  状态 
    el-form的footer点击确定按钮,清空表单
    重新请求表单数据
    -->
    <el-button @click="openAddDialog">新增数据</el-button>
    <el-dialog title="新增数据" :visible.sync="dialogVisible">
      <!-- 表单验证
      第一步:在el-form定义rules属性 -->
      <el-form
        ref="form"
        label-width="80px"
        :model="formData"
        :rules="formRules"
      >
        <el-form-item label="问题内容" prop="questionContent">
          <el-input v-model="formData.questionContent"></el-input>
        </el-form-item>

        <el-form-item label="问题回答" prop="questionAnswer">
          <el-input v-model="formData.questionAnswer"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <!-- <el-input v-model="formData.status"></el-input> -->

          <el-select v-model="formData.status">
            <el-option
              v-for="(item, index) in options"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button @click="confirm" type="primary">确定</el-button>
      </span>
    </el-dialog>

    <!-- 模糊搜索 
      要求:1.要求使用el-form实现搜索栏
            问题内容   questionContent
            状态       status
            搜索和重置按钮
           2.要求使用searchFormData对象来装 questionContent,status
           3.将所获得的questionContent,status属性的值,集成到getData接口中去
      -->
    <el-form
      :inline="true"
      :model="searchFormData"
      ref="searchForm"
      label-width="100px"
    >
      <el-form-item label="问题内容" prop="questionContent">
        <el-input
          v-model="searchFormData.questionContent"
          placeholder="请输入问题内容"
        ></el-input>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-input
          v-model="searchFormData.status"
          placeholder="请输入状态"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getData">搜索</el-button>
        <el-button type="info" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    {{ searchFormData }}

    <hr />
    <el-table :data="tableData">
      <el-table-column prop="questionId" label="问题ID"></el-table-column>
      <el-table-column
        prop="questionContent"
        label="问题内容"
      ></el-table-column>
      <el-table-column prop="questionAnswer" label="问题回复"></el-table-column>

      <!-- 选择器 -->
      <el-table-column prop="status" label="状态">
        <!-- <el-select v-model="formData.status"> -->
        <!-- <el-option label="1" value="1"></el-option>
          <el-option label="2" value="2"></el-option> -->

        <template slot-scope="scope">
          {{ scope.row.status | filterStatus(options) }}
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <!-- 修改数据
          参数:questionId  questionContent  questionAnswer status
          点击编辑按钮,打开dialog对话框
          拿到当前行的数据,赋值给表单,回显当前行的数据
          对数据修改
          点击确定,调用修改接口
          修改完成后,关闭el-dialog对话框,清空表单
          重新请求表单数据
          -->
          <el-button type="text" size="small" @click="upDataRow(scope.row)"
            >编辑</el-button
          >

          <el-button type="text" size="small" @click="deleteRow(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 30, 100]"
      :page-size="pageSize"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import axios from "axios";
export default {
  filters: {
    filterStatus(value, options) {
      let index = options.findIndex((item) => {
        return value === item.value;
      });
      return options[index].label;
    },
  },
  mounted() {
    // dom挂载之后执行
    this.getData();
  },
  data() {
    return {
      //表单验证第二步,在data中定义属性,
      // Form-Item 的 prop 属性设置为需校验的字段名
      //change,当内容改变时触发校验
      //blur,当失去焦点时触发
      formRules: {
        questionContent: [
          { required: true, trigger: "change", message: "问题内容为必填项" },
        ],
        questionAnswer: [
          { required: true, trigger: "blur", message: "问题回答需要填写" },
        ],
        status: [{ required: true, trigger: "change", message: "请选择状态" }],
      },
      selValue: "",
      options: [
        { label: "1", value: "1" },
        { label: "2", value: "2" },
      ],
      searchFormData: {
        questionContent: "",
        status: "",
      },
      dialogVisible: false,
      dialogType: "",
      tableData: [],
      pageIndex: 1,
      pageSize: 10,
      total: 0,
      formData: {
        questionId: "",
        questionContent: "",
        status: "",
        questionAnswer: "",
      },
    };
  },
  methods: {
    //修改数据方法
    upDataRow(row) {
      this.dialogType = "edit";
      this.dialogVisible = true;
      // 深拷贝

      // nextTick  解决表单清空问题
      // 官网解释，在下次dom更新之前执行
      //等嗲数据操作完成之后,在dom完全更新之后
      this.$nextTick(() => {
        this.formData = Object.assign({}, row);
      });
      // this.formData = { ...this.formData };
    },

    // 打开对话框方法
    openAddDialog() {
      this.dialogType = "add"; //设置独立标识,用来辨别是新增按钮还是修改按钮
      this.dialogVisible = true;
    },
    //切换页码触发的方法
    handleCurrentChange(value) {
      console.log("当前页码", value);
      this.pageIndex = value;

      //第三步,处理pageIndex的变动
      this.getData();
    },

    //分页的条数变动时触发的方法
    handleSizeChange(value) {
      console.log("当前每页" + value + "条");
      this.pageSize = value;

      //处理pageSize的变动
      this.getData();
    },

    //表格添加数据
    getData() {
      //第一步,替换动态参数,pageIndex,pageSize
      axios
        .get("http://127.0.0.1:3000/api/test/crud/list", {
          params: {
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            questionContent: this.searchFormData.questionContent,
            status: this.searchFormData.status,
          },
        })
        .then((res) => {
          //   console.log(res);
          (this.tableData = res.data.data),
            //第二步,动态赋值分页组件的total
            (this.total = res.data.total);
        });
    },

    //重置按钮
    reset() {
      this.$refs.searchForm.resetFields();
      //刷新表单
      this.getData();
    },

    //删除按钮
    deleteRow(row) {
      // console.log(row);
      this.$confirm("确定删除该条数据吗?").then(() => {
        axios
          .delete("http://127.0.0.1:3000/api/test/crud/delete", {
            params: {
              questionId: row.questionId,
            },
          })
          .then((res) => {
            console.log(res);
            this.getData();
          });
      });
    },

    // 取消按钮
    cancel() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },

    //确定按钮
    confirm() {
      // 表单验证第三步
      this.$refs.form.validate((valid) => {
        if (valid) {
          //逻辑代码
          // 点击新增数据时弹出的对话框
          if (this.dialogType === "add") {
            axios
              .post("http://127.0.0.1:3000/api/test/crud/add", {
                ...this.formData,
              })
              .then((res) => {
                console.log(res);
                this.dialogVisible = false;
                this.$refs.form.resetFields();
                this.getData();
              });
          } else if (this.dialogType === "edit") {
            // 点击更改数据时弹出的对话框
            axios
              .put("http://127.0.0.1:3000/api/test/crud/update", {
                ...this.formData,
              })
              .then((res) => {
                console.log(res);
                this.dialogVisible = false;

                this.$refs.form.resetFields();

                this.getData();
              });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>